package bTree.LeetCode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class 组合总和 {
    List<List<Integer>>res=new LinkedList<>();
    List<Integer>temp=new ArrayList<>();
    int sum=0;
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        backtracking(candidates,target,0);
        return res;
    }

    private void backtracking(int[] candidates, int target, int startindex) {
        if(sum==target){
            res.add(new ArrayList<>(temp));
            return;
        }
        if(sum>target){
            return;
        }

        for(int i=startindex;i<candidates.length;i++){
            sum+=candidates[i];
            temp.add(candidates[i]);
            backtracking(candidates,target,i);
            sum-=candidates[i];
            temp.remove(temp.size()-1);
        }
    }

}